package com.qiuling.iot.rabbit;


import com.qiuling.iot.config.RabbitConfig;
import com.qiuling.iot.entity.EquipmentData;
import com.qiuling.iot.mapper.EquipmentDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author：zhangqiang
 * @Description：消息监听
 * @Date：Created in 18:40 2020/3/12
 * @Modified By：
 */
@Component
@Slf4j
public class MessageListener {

    @Autowired
    private EquipmentDataMapper equipmentDataMapper;

    /**
     * @description 监听设备发送信息
     * </br>       
     * @param equipmentData
     * @return void
     * @author zhangqiang
     * @date 21:52 2020/3/12
     */
    @RabbitListener(queues = RabbitConfig.DATA_QUEUE, containerFactory = "singleListenerContainer")
    public void insertData(@Payload EquipmentData equipmentData) {

        log.info("监听设备发送信息：{}, 设备编码: {}", equipmentData.getSendData(), equipmentData.getEquipmentCode());
        //插入设备数据表
        equipmentData.setSendTime(new Date());
        equipmentData.setDisabled(1);
        int insert = equipmentDataMapper.insert(equipmentData);
        if(insert == 0) {
            log.error("发送数据插入失败");
        }

    }

}
